<template>
  <lc-crud
    ref="curd"
    :default-sort="{
      prop: 'createTime',
      order: 'ascending'
    }"
    :column="column"
    :record="record"
    :search="search"
    :option="option"
    :noCache="true"
    :operator="operator"
    :size="'default'"
    :search-span="6"
    :data="`/lc-system/permission?menuId=${props?.i.menuId}`"
  />
</template>
<script setup lang="ts" name="sys-permission">
import { TCrudComponent, TRecord } from "@/components/crud/interface";
import {
  ITableColumn,
  STExposeComponent,
  TOporaterOption
} from "@/components/st/interface";
import LcCrud from "@/components/crud/crud.vue";
import { ref } from "vue";
import { NzSafeAny } from "@/components/sf/type";
import { RadioUI } from "@/components/sf/interface";
import { Http } from "@/core/api/http.service";
import { useAclHooks } from "@/core/pinia/store/acl.store";
const props = defineProps<{
  i: Record<string, NzSafeAny>;
}>();
const curd = ref<TCrudComponent>(null);
const acls = useAclHooks();
const operator = {
  delCode: "permission_delete",
  addCode: "permission_post"
} as TOporaterOption;
const search = {
  properties: {
    name: {
      title: "名称",
      ui: {
        placeholder: "请输入名称",
        grid: {
          spanLabel: 50
        }
      }
    }
  }
};
const option = {
  title: "菜单权限点"
};
const record = {
  schema: {
    properties: {
      menuId: {
        type: "string",
        title: "名称",
        ui: {
          placeholder: "请输入账号",
          hidden: true
        },
        default: props.i.menuId
      },
      name: {
        type: "string",
        title: "名称",
        ui: {
          placeholder: "请输入账号",
          grid: {
            span: 13
          }
        }
      },
      code: {
        type: "string",
        title: "代码",
        readonly: (data: any) => {
          return !!data?.id;
        },
        ui: {
          placeholder: "请输入代码",
          grid: {
            span: 11
          },
          asyncValidate: async (val: string) => {
            return Http.of().debVaildGet(
              `/lc-system/permission/exits/${val}/${props.i.menuId}`
            );
          },
          errorMsg: "当前key已存在"
        }
      },
      forbidden: {
        type: "boolean",
        title: "禁用",
        ui: {
          widget: "radio",
          border: true,
          items: [
            { label: "否", value: false },
            { label: "是", value: true }
          ]
        } as RadioUI,
        default: false
      },
      remark: {
        type: "string",
        title: "备注",
        ui: {
          widget: "textarea",
          maxLength: 300,
          placeholder: "备注"
        }
      }
    },
    required: ["name", "code", "forbidden", "remark"]
  }
} as TRecord;
const column = [
  {
    type: "selection",
    prop: "id"
  },
  {
    type: "index"
  },
  {
    prop: "name",
    label: "名称"
  },
  {
    prop: "code",
    label: "代码"
  },
  {
    label: "是否禁用",
    prop: "forbidden",
    formatter: ({ forbidden }) => {
      return forbidden ? "是" : "否";
    }
  },
  {
    prop: "remark",
    showOverflowTooltip: true,
    label: "备注"
  },
  {
    prop: "createTime",
    type: "date",
    sortable: true,
    dateFormat: "YYYY/MM/DD hh",
    label: "创建时间"
  },
  {
    label: "操作",
    acl: {
      points: acls.getAclArray("permission_patch", "permission_delete"),
      mode: "oneOf"
    },
    buttons: [
      {
        text: "编辑",
        icon: "ant-design:edit-filled",
        acl: { points: [acls.getMenuAcl("permission_patch")] },
        click: (data: NzSafeAny, st: STExposeComponent) =>
          curd.value?.update(data).subscribe(() => st.reset())
      },
      {
        text: "删除",
        type: "del",
        icon: "ant-design:delete-filled",
        divider: "dashed",
        acl: { points: [acls.getMenuAcl("permission_delete")] },
        click: (data: NzSafeAny, st: STExposeComponent) =>
          curd.value?.delOne(data.id).subscribe(() => st.reset())
      }
    ]
  }
] as ITableColumn[];
</script>
@/components/st/interfaces @/components/crud/interfaces
@/components/sf/interfaces @/components/sf/types
